package com.shop2cn.iapi.sqmarketing.testcase;

import java.math.BigDecimal;
import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqmarketing.parameter.SaveOrUpdaateMarketingActivityReq;
import com.shop2cn.iapi.sqmarketing.pojo.ActivityConditionItemPojo;
import com.shop2cn.iapi.sqmarketing.service.SaveOrUpdaateMarketingActivityCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqmarketing.ActivityConditionItem;
import com.ymttest.database.model.sqmarketing.MarketingActivity;
import com.ymttest.database.sqlwapper.sqmarketing.ActivityConditionItemWrapper;
import com.ymttest.database.sqlwapper.sqmarketing.MarketingActivityWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;

public class Ts_SaveOrUpdaateMarketingActivity {

	// java接口
	private static SaveOrUpdaateMarketingActivityReq saveOrUpdaateMarketingActivityReq;
	private static SaveOrUpdaateMarketingActivityCall saveOrUpdaateMarketingActivityCall;
	
	private static ActivityConditionItemWrapper activityConditionItemWrapper = new ActivityConditionItemWrapper();
	private static MarketingActivityWrapper marketingActivityWrapper = new MarketingActivityWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("新增或更新营销活动");
	}

	@Before
	public void caseUp() {
		saveOrUpdaateMarketingActivityReq = new SaveOrUpdaateMarketingActivityReq();
		saveOrUpdaateMarketingActivityCall = new SaveOrUpdaateMarketingActivityCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(saveOrUpdaateMarketingActivityCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb() throws Exception {
		
		MarketingActivity actualMarketingActivity = marketingActivityWrapper
				.selectBySellerId(saveOrUpdaateMarketingActivityReq
						.getSellerId());
		
		String couponBatchIdString = "";
		
		for (int i = 0; i < saveOrUpdaateMarketingActivityReq
				.getCouponBatchIds().size(); i++) {

			if (i == saveOrUpdaateMarketingActivityReq.getCouponBatchIds()
					.size() - 1)
				couponBatchIdString = couponBatchIdString
						+ saveOrUpdaateMarketingActivityReq.getCouponBatchIds()
								.get(i);
			else
				couponBatchIdString = couponBatchIdString
						+ saveOrUpdaateMarketingActivityReq.getCouponBatchIds()
								.get(i) + ";";
		}
		
		Logger.comment("验证marketing_activity表");
		
		Logger.verifyEquals(
				saveOrUpdaateMarketingActivityReq.getActivityName(),
				actualMarketingActivity.getActivity_name(), "验证Activity_name");
		Logger.verifyEquals(saveOrUpdaateMarketingActivityReq.getSellerId(),
				actualMarketingActivity.getSeller_id(), "验证Seller_id");
		Logger.verifyEquals(saveOrUpdaateMarketingActivityReq
				.getConditionType().byteValue(), actualMarketingActivity
				.getCondition_type(), "验证Condition_type");
		Logger.verifyEquals(saveOrUpdaateMarketingActivityReq.getCountLimit(),
				actualMarketingActivity.getCount_limit(), "验证Count_limit");
		DataUtil.verifyDecimalEquals(
				saveOrUpdaateMarketingActivityReq.getPayAmountThreshold(),
				actualMarketingActivity.getPay_amount_threshold(),
				"验证Pay_amount_threshold");
		Logger.verifyEquals(saveOrUpdaateMarketingActivityReq.getPayCurrency(),
				actualMarketingActivity.getPay_currency(), "验证Pay_currency");
		DataUtil.verifyDateEquals(
				saveOrUpdaateMarketingActivityReq.getStartTime(),
				actualMarketingActivity.getStart_time(), "验证Start_time");
		DataUtil.verifyDateEquals(
				saveOrUpdaateMarketingActivityReq.getEndTime(),
				actualMarketingActivity.getEnd_time(), "验证End_time");
		Logger.verifyEquals(saveOrUpdaateMarketingActivityReq
				.getActivityStatus().byteValue(), actualMarketingActivity
				.getActivity_status(), "验证Activity_status");
		Logger.verifyEquals(couponBatchIdString,
				actualMarketingActivity.getCoupon_batch_ids(),
				"验证Coupon_batch_ids");
		Logger.verifyEquals((byte) 1, actualMarketingActivity.getDelete_flag(),
				"验证Delete_flag");

		ActivityConditionItem actualActivityConditionItem = activityConditionItemWrapper
				.selectByActivityId(actualMarketingActivity.getId());
		
		ActivityConditionItemPojo activityConditionItemPojo = saveOrUpdaateMarketingActivityReq.getActivityConditionItemList().get(0);
		
		String itemString = "";
		
		for (int i = 0; i < activityConditionItemPojo.getItemList().size(); i++) {

			if (i == activityConditionItemPojo.getItemList().size() - 1)
				itemString = itemString
						+ activityConditionItemPojo.getItemList().get(i);
			else
				itemString = itemString
						+ saveOrUpdaateMarketingActivityReq.getCouponBatchIds()
								.get(i) + ",";
		}
		
		Logger.comment("验证activity_condition_item表");

		Logger.verifyEquals(itemString,
				actualActivityConditionItem.getActivit_condition_item(),
				"验证Activit_condition_item");
		Logger.verifyEquals(activityConditionItemPojo.getConditionType()
				.byteValue(), actualActivityConditionItem.getCondition_type(),
				"验证Condition_type");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveOrUpdaateMarketingActivity_001() {
		Logger.start(true, "新增营销活动");

		try {
			
			ActivityConditionItemPojo activityConditionItemPojo = new ActivityConditionItemPojo();
			
			activityConditionItemPojo.setItemList(Arrays.asList("wjehrkwhe"));
			activityConditionItemPojo.setConditionType(1);

//			saveOrUpdaateMarketingActivityReq.setId(id);
			saveOrUpdaateMarketingActivityReq.setSellerId((long)TestDataManager.getRandomIntNum(8));
			saveOrUpdaateMarketingActivityReq.setPayCurrency("CNY");
			saveOrUpdaateMarketingActivityReq.setActivityName("activityName");
			saveOrUpdaateMarketingActivityReq.setActivityStatus(1);
			saveOrUpdaateMarketingActivityReq.setActivityConditionItemList(Arrays.asList(activityConditionItemPojo));
			saveOrUpdaateMarketingActivityReq.setStartTime("2021-08-03 17:21:05");
			saveOrUpdaateMarketingActivityReq.setEndTime("2022-08-03 17:21:05");
			saveOrUpdaateMarketingActivityReq.setCouponBatchIds(Arrays.asList("gshdghsdkg"));
			saveOrUpdaateMarketingActivityReq.setConditionType(1);
			saveOrUpdaateMarketingActivityReq.setPayAmountThreshold(BigDecimal.TEN);
			saveOrUpdaateMarketingActivityReq.setCountLimit(1);

			saveOrUpdaateMarketingActivityCall.setData(saveOrUpdaateMarketingActivityReq);
			saveOrUpdaateMarketingActivityCall.callService();

			Logger.verifyEquals(true, saveOrUpdaateMarketingActivityCall.getSuccess(),
					"验证success");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveOrUpdaateMarketingActivity_002() {
		Logger.start(true, "新增营销活动，多个batchId");

		try {
			
			ActivityConditionItemPojo activityConditionItemPojo = new ActivityConditionItemPojo();
			
			activityConditionItemPojo.setItemList(Arrays.asList("wjehrkwhe"));
			activityConditionItemPojo.setConditionType(4);

//			saveOrUpdaateMarketingActivityReq.setId(id);
			saveOrUpdaateMarketingActivityReq.setSellerId((long)TestDataManager.getRandomIntNum(8));
			saveOrUpdaateMarketingActivityReq.setPayCurrency("CNY");
			saveOrUpdaateMarketingActivityReq.setActivityName("activityName");
			saveOrUpdaateMarketingActivityReq.setActivityStatus(1);
			saveOrUpdaateMarketingActivityReq.setActivityConditionItemList(Arrays.asList(activityConditionItemPojo));
			saveOrUpdaateMarketingActivityReq.setStartTime("2021-08-03 17:21:05");
			saveOrUpdaateMarketingActivityReq.setEndTime("2022-08-03 17:21:05");
			saveOrUpdaateMarketingActivityReq.setCouponBatchIds(Arrays.asList("gshdghsdkg", "gjldsjfkljs", "uejyergdjflg"));
			saveOrUpdaateMarketingActivityReq.setConditionType(1);
			saveOrUpdaateMarketingActivityReq.setPayAmountThreshold(BigDecimal.ZERO);
			saveOrUpdaateMarketingActivityReq.setCountLimit(1);

			saveOrUpdaateMarketingActivityCall.setData(saveOrUpdaateMarketingActivityReq);
			saveOrUpdaateMarketingActivityCall.callService();

			Logger.verifyEquals(true, saveOrUpdaateMarketingActivityCall.getSuccess(),
					"验证success");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
